/**
 * 2018年11月21日下午4:34:02
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.PathKit;
import com.baihang.project.ds.service.FangYuanService;
/**
 * FangYuan 房源
 */
@RequestMapping("/fangyuan")
@Controller
public class FangYuanController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(FangYuanController.class);
	@Autowired
	private FangYuanService service;
	@RequestMapping
	public String index() {

		return "/project/ds/fangyuan/index";
	}
	
	@RequestMapping(value ="/getGrid", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getGrid(int page,int pagesize,String sortname,String sortorder,String where) {
		String grid = service.getGrid(page,pagesize,sortname,sortorder,where);
		//System.out.println(grid);
		return grid;
	}
	
	@RequestMapping(value ="/saveGrid", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String saveGrid(String json) {
		String result ="true";
		if(!service.saveGrid(json)) {
			result="false";
		}		
		return String.format("{\"result\":\"%s\"}", result);
	}
	/**
	 * 新增房源
	 * @param json
	 * @param banname
	 * @param nodeId
	 * @param nodeType
	 * @param nodeOrder
	 * @return
	 */
	@RequestMapping(value ="/addGrid", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String addGrid(String json,String banname,int nodeId,String nodeType,String nodeOrder) {
		String result ="true";
		String msg ="新增成功";
		if(!service.addGrid(json,banname,nodeId,nodeType,nodeOrder)) {
			result="false";
			msg="新增失败";
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
	
	@RequestMapping("/downloadFangYuanByLouDong")
	public ResponseEntity<byte[]> downloadFangYuanByLouDong(String louDongName) throws Exception{
		
		return service.downloadFangYuanByLouDong(louDongName);
	}
	
	/**
	 * 导出电视列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/downloadFangYuanForDianShi")
	public ResponseEntity<byte[]> downloadFangYuanForDianShi() throws Exception{
		
		return service.downloadFangYuanForDianShi();
	}
	
	/**
	 * 上传excel表保存到本地
	 * @param file
	 * @return
	 */
	@RequestMapping(value ="/uploadFangYuanByLouDong", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String uploadFangYuanByLouDong(MultipartFile roomexcel) {
		String filepath = PathKit.getWebRootPath()+"/upload/"+roomexcel.getOriginalFilename();
		System.err.println(filepath);
		try {
			roomexcel.transferTo(new File(filepath));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.format("{\"result\":\"%s\",\"fileName\":\"%s\"}", "true",roomexcel.getOriginalFilename());
	}
	/**
	 * 预览上传的excel
	 * @return
	 */
	@RequestMapping("/uploadRoomPreview")
	public String uploadRoomPreview() {
		
		return "/project/ds/fangyuan/uploadRoomPreview";
	}
	
	/**
	 * 读取excel返回json
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/readFangYuanByExcel", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String readFangYuanByExcel(String fileName) throws Exception {
		System.err.println(fileName);
		String result = service.readFangYuanByExcel(fileName);
		return result;
	}
	
	@RequestMapping("/downFangYuan2")
	@ResponseBody
	public void downFangYuan2(HttpServletResponse response) throws Exception {
		service.downFangYuan2(response);				
	}
	
	@RequestMapping(value ="/deleteRoom", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteRoom(String banname) throws Exception {
		String result ="false" ;
		String msg ="删除失败";
		if(service.deleteRoom(banname)) {
			result ="true";
			msg="删除成功";
		}
		return JsonKit.toDoubleJson(result, msg);
	}
}
